package post_to_in_pkg.main;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;

import post_to_in_pkg.lexer.Lexer;
import post_to_in_pkg.parser.Parser;
import post_to_in_pkg.recoder.Recoder_v1;
import javafx.application.*;
import javafx.geometry.Insets;
import javafx.geometry.Orientation;
import javafx.scene.control.*;
import javafx.stage.*;
import javafx.stage.FileChooser.ExtensionFilter;
import javafx.scene.layout.FlowPane;
import javafx.scene.Scene;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import java.io.FileWriter;
public class Main extends javafx.application.Application{
	
	static Button import_Btn, export_Btn, commitBtn;
	static TextField outputDir_tf, inputDir_tf;
	static TextArea view_tf;
	public void start(Stage PrimaryStage) {
		FlowPane MainPane=new FlowPane(Orientation.VERTICAL);
		MainPane.setPadding(new Insets(15,4,4,12));
		MainPane.setVgap(4);
		FlowPane pane1=new FlowPane(Orientation.HORIZONTAL);
		FlowPane pane2=new FlowPane(Orientation.HORIZONTAL);
		Label label=new Label("Script View:");
		
		
		//pane1 
		inputDir_tf=new TextField();
		inputDir_tf.setPrefWidth(400);
	    import_Btn=new Button("import");
		pane1.setHgap(5); pane1.setMinHeight(25);
		pane1.setPrefHeight(pane1.getMinHeight());
		pane1.getChildren().addAll(inputDir_tf,import_Btn);
		//pane2
		outputDir_tf=new TextField();
		outputDir_tf.setPrefWidth(400);
		export_Btn=new Button("Export");
		pane2.setHgap(5); pane2.setMinHeight(25);
		pane2.setPrefHeight(pane2.getMinHeight());
		pane2.getChildren().addAll(outputDir_tf,export_Btn);
		
		//main pane
		view_tf=new TextArea();
		view_tf.setPrefSize(500,400);
		//view_tf.setEditable(false);
		commitBtn=new Button("commit");
		MainPane.getChildren().addAll(pane1,pane2,commitBtn,label,view_tf);
		
		//set the stage
		Scene scene=new Scene(MainPane,530,550);
		PrimaryStage.setTitle("BitcoinScript Transfromation");
		PrimaryStage.setScene(scene);
		PrimaryStage.setResizable(false);
		//Scene scene=new Scene(,200,300);
		
		//subscribe handler
		BtnPressedHandler BtnHandler=new BtnPressedHandler();
		commitBtn.setOnAction(BtnHandler);
		export_Btn.setOnAction(BtnHandler);
		import_Btn.setOnAction(BtnHandler);
		
		//set viewTextField properties t
		//view_tf.
		PrimaryStage.show();
	}
	
	public static void process(String in,String out) throws Exception {
		if(out==null)
			out=in.substring(0, in.indexOf('.'))+"_out.txt";
		File inFile=new File(in);
		if(!inFile.exists())
			throw new IOException("File not exist");
		if(!inFile.canRead()) {
			throw new IOException("File cannot be read");
		}
		FileWriter fw=new FileWriter(out);
		lex=new Lexer(inFile);
		parser=new Parser(lex);
		Recoder.setParser(parser);
		parser.makeSyntaxTree();
		fw.write(
		Recoder.writeInBuffer().toString()
		);
		fw.close();
		
	}
	
	static Lexer lex;
	static Parser parser;
	static Recoder_v1 Recoder=new Recoder_v1();
	
	static String help="		[options] <fileInputPath> <file>\r\n" + 
			"		-a process all file in this directory, <disable>\r\n" + 
			"		-q quiet mode \r\n" + 
			"		-p print output \r\n" + 
			"		-h ask for help\r\n" + 
			"		-u start simple UI ";
	
	public static String main(String[] args) throws Exception {	
		int argc=args.length;
		String[] retStr=new String[2];
		try {
			switch(argc) {
			case 2: {
				if(args[0].charAt(0)!='-') {
					process(args[0],args[1]); resetState();
				}
				//process other option
				else if(args[0].equals("-p")) {
					process(args[0],null);
				}
				else if(args[0].equals("--overwrite-existing")) {
					process(args[1],"temp2_out.txt");
					retStr[0]="temp2_out.txt";
				}
			}
			break;
			case 1: {
				if(args[0].equals("-u")) {
					Application.launch(args);
				}
				if(args[0].equals("-h")) {
					System.out.print(help);
				}
				else if(!args[0].startsWith("-")) {
					process(args[0],null);resetState();
				}

			}break;
			case 3: {
				if(args[0].equals("-p")) {
					process(args[1],args[2]);
					System.out.print(Recoder.writeInBuffer());
					resetState();
				}
				else {
					System.out.println("invalid arguments");
				}
			}break;
			default:
				System.out.println("invalid arguments,at least 1 argument is require.");
				//System.out.println("Type"+" -h for help");
				
				break;
			}
		}
		catch(Exception e) {
			if(args[0].contains("q")) {
				System.exit(1);
			}
			e.printStackTrace();//abnormal exit
			
			//retStr[1]=e.getMessage();
			//return retStr[];
			//System.exit(1); //abnormal exit
			//System.out.println(e.getMessage().toString());
			
			throw e;
		}
		
		return retStr[0];
		//System.exit(0);
	}
	
	private static void resetState() {
		parser.resetParser(); parser=null;
	}
	
}

